package com.qf.service.impl;

import com.qf.dao.*;
import com.qf.dao.impl.*;
import com.qf.entity.*;
import com.qf.service.OrderService;
import com.qf.util.CodeUtil;
import com.qf.util.DbUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @Author AzureSky_X
 * @Date 2020/11/19 17:23
 * @Version 1.0
 */
public class OrderServiceImpl implements OrderService {

    private OrderDao dao=new OrderDaoImpl();
    private GoodCarDao goodCarDao=new GoodCarDaoImpl();
    private OrderDetailDao orderDetailDao=new OrderDetailDaoImpl();
    private AddressDao addressDao=new AddressDaoImpl();
    private GoodDao goodDao =new GoodDaoImpl();
    @Override
    public int addOrder(Order order) {
        try{
            DbUtils.begin();
            //1.向tb_order表中插入数据
            order.setOrderNum(CodeUtil.getOrderNum());
            order.setOrderStatus(0);//默认为0,就是未付款
            order.setOrderTime(new Date());
            //注意，接下来插入订单详情时需要订单的id,订单id主键自动生成的，要获取到自增长id
            dao.insertOrder(order);

            //2.往tb_OrderDetail中插入数据
            //tb_OrderDetail中的数据表示的是购物车选项中的数据
            List<GoodCar> goodCars = goodCarDao.selectGoodCarsByUid(order.getUid());
            //将当前用户下的购物车选项封装成订单详情选项
            List<OrderDetail> list=new ArrayList<>();
            for(GoodCar car:goodCars){
                OrderDetail orderDetail=new OrderDetail();
                orderDetail.setOid(order.getId());//订单id
                orderDetail.setGid(car.getGoodId());
                orderDetail.setNum(car.getGoodNum());
                orderDetail.setSum(car.getGoodSum());
                list.add(orderDetail);
            }

            for(OrderDetail od:list){
                orderDetailDao.insertOrderDetail(od);
            }

            //3.清空购物车

            goodCarDao.deleteGoodCarByUid(order.getUid());

            DbUtils.commit();
            return 1;
        }catch (Exception e){
            DbUtils.rollback();
            e.printStackTrace();
            return 0;
        }

    }

    @Override
    public List<Order> getOrderList(Integer id) {
        List<Order> list=null;
        try{
            DbUtils.begin();
            list=dao.selectOrdersByUid(id);
            //由于前端页面需要地址的内容，所以我们需要根据aid查出地址对象
            for(Order o:list){
                Address address= addressDao.selectAddressesById(o.getAid());
                o.setAddress(address);
            }
            DbUtils.commit();
        }catch(Exception e){
            DbUtils.rollback();
            e.printStackTrace();

        }
        return list;
    }

    @Override
    public Order getOrder(int id) {
        Order order=null;
        try {
            DbUtils.begin();
            order=dao.selectOrdersById(id);
            //根据查询到的aid,得到address对象
            Address address = addressDao.selectAddressesById(order.getAid());
            order.setAddress(address);
            //根据订单id,查出该订单有几个订单项
            List<OrderDetail> list= orderDetailDao.selectOrderDetailsByOid(order.getId());
            //根据OrderDetail中的gid,查询出对于good对象
            for(OrderDetail od:list){
                Good good = goodDao.selectGoodById(od.getGid());
                od.setGood(good);
            }
            order.setOrderDetails(list);

            DbUtils.commit();
        }catch (Exception e){
            DbUtils.rollback();
            e.printStackTrace();
        }
        return order;
    }
}
